package de.preissler.insertselection;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

public class InsertQuotesCalc implements InsertQuotes
{

	public void insertQuotes(Map<String, Map<String, Quote>> quotes,
			Keyspace keyspace, Mutator<String> mutator)
	{
		insertQuotesInBase(quotes, keyspace, mutator);
		mutator.execute();
	}
	
	private void insertQuotesInBase(Map<String, Map<String, Quote>> quotes, Keyspace keyspace, Mutator<String> mutator)
	{
		for(String isin : quotes.get(quotes.keySet().iterator().next()).keySet())
		{
			for(String date : quotes.keySet())
			{
				List<HColumn<String, Double>> columnList = new LinkedList<HColumn<String, Double>>();
				
				//Werte holen
				columnList.add(HFactory.createColumn(CHANGE, quotes.get(date).get(isin).change, SS, DS));
				columnList.add(HFactory.createColumn(CLOSE, quotes.get(date).get(isin).close, SS, DS));
				columnList.add(HFactory.createColumn(HIGH, quotes.get(date).get(isin).high, SS, DS));
				columnList.add(HFactory.createColumn(LOW, quotes.get(date).get(isin).low, SS, DS));
				columnList.add(HFactory.createColumn(OPEN, quotes.get(date).get(isin).open, SS, DS));
				columnList.add(HFactory.createColumn(VOLUME, quotes.get(date).get(isin).volume, SS, DS));
				
				mutator.addInsertion(isin, QUOTES, HFactory.createSuperColumn(date, columnList, SS, SS, DS));
			}
		}
	}
	
	
	public void deleteQuotes(Map<String, Map<String, Quote>> quotes,
			Keyspace keyspace, Mutator<String> mutator)
	{
		deleteQuotesinBase(quotes, keyspace, mutator);
		mutator.execute();
		
		//Compaction in Cassandra ausfuehren
		JMX.clean(Main.IP, Main.KEYSPACE_NAME, InsertQuotes.QUOTES);
		
	}

	private void deleteQuotesinBase(Map<String, Map<String, Quote>> quotes,
			Keyspace keyspace, Mutator<String> mutator)
	{
		for(String isin : quotes.get(quotes.keySet().iterator().next()).keySet())
		{
			for(String date : quotes.keySet())
			{
				mutator.addDeletion(isin, QUOTES, date, SS);
			}
		}
	}

}
